﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
    class DbHelper
    {
        /// <summary>
        /// 获得表数据
        /// </summary>
        /// <param name="sql">查询select语句</param>
        /// <returns>表数据</returns>
        private static readonly  string conString = "server=.;database=Blogs;uid=sa;pwd=azhana824;";

        public static DataTable GetDataTable(string sql)
        {          
            SqlConnection connection = new SqlConnection(conString);       

            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql,connection);

            DataTable dataTable = new DataTable();

            connection.Open();
            dataAdapter.Fill(dataTable);

            connection.Close();
            return dataTable;
        }
        /// <summary>
        /// 增加或修改的方法
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int AddOrEditOrDeleteSave(string sql)
        {
            SqlConnection connection = new SqlConnection(conString);

            SqlCommand command = new SqlCommand(sql, connection);

            connection.Open();

            return  command.ExecuteNonQuery();
        }
    }
}
